package com.niiwoo.civet.user.service.local;

import com.niiwoo.civet.user.dao.entity.UserForbidInfo;
import com.niiwoo.civet.user.dao.entity.UserForbidType;
import com.niiwoo.civet.user.dao.mapper.UserForbidInfoMapperExt;
import com.niiwoo.civet.user.dao.mapper.UserForbidTypeMapperExt;
import com.niiwoo.civet.user.dto.UserForbidDubboInfoDTO;
import com.niiwoo.civet.user.dto.response.ValidUserForbidResponseDTO;
import com.niiwoo.civet.user.enums.UserForbidTypeEnum;
import com.niiwoo.civet.user.enums.UserStatusEnum;
import com.niiwoo.tripod.provider.exception.BizException;
import com.niiwoo.tripod.service.component.SnowflakeIdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: wuke
 * @Description:
 * @Date: Created in 15:49 2017/11/7
 */
@Slf4j
@Service
public class UserForbidService {
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private UserForbidInfoMapperExt ucForbidInfoMapperExt;
    @Autowired
    private UserForbidTypeMapperExt userForbidTypeMapper;

    /**
     * 查询当前时间有效的行为禁止类型信息
     *
     * @param userId
     * @param userForbidTypeEnum
     * @return
     */
    public ValidUserForbidResponseDTO queryValidUserForbidInfo(String userId, UserForbidTypeEnum userForbidTypeEnum) {
        Date now = new Date();
        UserForbidInfo userForbidInfo = ucForbidInfoMapperExt.queryOneValidUcForbidInfo(userId, userForbidTypeEnum, now);

        if (userForbidInfo == null) {
            return null;
        }

        ValidUserForbidResponseDTO result = new ValidUserForbidResponseDTO();
        BeanUtils.copyProperties(userForbidInfo, result);
        this.setForbidTypeInfo(userForbidInfo.getForbidBehaviorTypeId(), result);

        return result;
    }

    public void validUserForbidInfo(String userId, UserForbidTypeEnum userForbidTypeEnum) {
        ValidUserForbidResponseDTO validUserForbidResponseDTO = this.queryValidUserForbidInfo(userId, userForbidTypeEnum);
        if (null != validUserForbidResponseDTO) {
            log.info("用户：{}，被禁止{}", userId, userForbidTypeEnum.getDesc());
            throw new BizException("USR10007", validUserForbidResponseDTO.getTipsContent());
        }
    }

    //设置行为禁止类型信息
    private void setForbidTypeInfo(Long forbidBehaviorTypeId, ValidUserForbidResponseDTO validUserForbidResponseDTO) {
        if (forbidBehaviorTypeId == null) {
            return;
        }
        UserForbidType userForbidType = userForbidTypeMapper.selectByPrimaryKey(forbidBehaviorTypeId);
        if (userForbidType == null) {
            return;
        }
        validUserForbidResponseDTO.setForbidTypeName(userForbidType.getForbidTypeName());
        validUserForbidResponseDTO.setTipsContent(userForbidType.getTipsContent());
    }

    public UserForbidInfo queryUcForbidInfo(String userId, String forbidType) {
        Map<String, Object> queryUcForbidInfoParams = new HashMap<String, Object>();
        queryUcForbidInfoParams.put("userId", userId);
        queryUcForbidInfoParams.put("forbidType", forbidType);
        List<UserForbidInfo> ucForbidList = ucForbidInfoMapperExt.queryUcForbidInfo(queryUcForbidInfoParams);

        if (CollectionUtils.isEmpty(ucForbidList)) {
            return null;
        }
        UserForbidInfo userForbidInfo = ucForbidList.get(0);
        return userForbidInfo;
    }

    public void saveUserForbidInfo(UserForbidDubboInfoDTO userForbidDubboInfoDTO) {
        if (userForbidDubboInfoDTO == null) {
            return;
        }

        Long id = userForbidDubboInfoDTO.getId();
        Date now = new Date();

        UserForbidInfo ucForbidNew = new UserForbidInfo();
        BeanUtils.copyProperties(userForbidDubboInfoDTO, ucForbidNew);
        ucForbidNew.setStatus(UserStatusEnum.USER_FORBID_STATUS_VALID.getValue());
        ucForbidNew.setUpdateTime(now);

        if (id == null) { //id为空则插入
            ucForbidNew.setId(snowflakeIdWorker.nextId());
            ucForbidNew.setCreateTime(now);
            ucForbidInfoMapperExt.insertSelective(ucForbidNew);
        } else {
            ucForbidInfoMapperExt.updateByPrimaryKey(ucForbidNew);
        }
    }


    /**
     * 卡牛行为禁止
     * @param userId
     * @param userForbidTypeEnum
     * @return
     */
    public ValidUserForbidResponseDTO cardNiuvalidUserForbidInfo(String userId, UserForbidTypeEnum userForbidTypeEnum) {
        ValidUserForbidResponseDTO validUserForbidResponseDTO = this.queryValidUserForbidInfo(userId, userForbidTypeEnum);
       return validUserForbidResponseDTO;
    }
}
